/**
 * 登录、注册、忘记密码、修改密码、异常登录相关的js
 */

function timeoutRegister() {
    var timeoutRegister_btn = $("#reg_code");
    timeoutRegister_btn.addClass('disabled');
    var phone = $("#phone").val();
    if (phone == "") {
        alert("请输入手机号","提示");
        timeoutRegister_btn.removeClass('disabled');
        return;
    }
    //开始倒计时
    $.ajax({
        type: "POST",
        url: "/smsAjax",
        dataType: "json",
        data: {
        	phone: phone,
			type:$("#type").val()
		},
        success: function (ret) {
            if (ret.isOk) {
                countDownRegister();
            }if (ret.isFail) {
                alert(ret.msg);
                timeoutRegister_btn.removeClass('disabled');
            }
        }
    })
}
function countDownRegister() {
    var sTime = 60,     //倒计时的时间
        timer = null;
    var btn = $("#reg_code");
    clearTimeout(timer);
    timer = setTimeout(function () {
        if (sTime == 0) {
            btn.removeClass('disabled');
            btn.text("获取验证码");
            clearTimeout(timer);
            return false;
        } else {
            sTime--;
            setTimeout(arguments.callee, 1000);
        }
        btn.text(sTime + "/60s");
    }, 500);
}

function doRegister(flag) {
    var doRegist_btn = $("#login_btn");
    doRegist_btn.addClass('disabled');
    var phone = $("#phone").val();
    var password = $("#password").val();
    var rePassword = $("#rePassword").val();
    var code = $("#code").val();
    var recPhone = $("#recPhone").val();

    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(17[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
    if (phone == '') {
        alert("请输入手机号");
        doRegist_btn.removeClass('disabled');
        return;
    }
    if(!myreg.test(phone)){
        alert("请输入正确的手机号");
        doRegist_btn.removeClass('disabled');
        return;
    }
    if (code == '') {
        alert("请输入验证码");
        doRegist_btn.removeClass('disabled');
        return;
    }
    if (password == '') {
        alert("请输入登录密码");
        doRegist_btn.removeClass('disabled');
        return;
    }
    if (rePassword == '') {
        alert("请输入确认密码");
        doRegist_btn.removeClass('disabled');
        return;
    }

    if (rePassword != password) {
        alert("两次登录密码输入不一致");
        doRegist_btn.removeClass('disabled');
        return;
    }

    $.ajax({
        type : "POST",
        url : "/doRegister",
        contentType : "application/x-www-form-urlencoded; charset=utf-8",
        dataType : "json",
        data : {
            phone : phone,
            code : code,
            password : password,
            recPhone : recPhone
        },
        success : function(ret) {
            if (ret.isOk) {
                if('code'==flag){
                    alert("注册成功");
                }else{
                    alert("注册成功");
                }
                location.href = "/";
            }if (ret.isFail) {
                alert(ret.msg);
                doRegist_btn.removeClass('disabled');
            }
        }
    })
}

function resetPwd() {
    var btn = $("#login_btn");
    btn.addClass('disabled');
    var phone = $("#phone").val();
    var password = $("#password").val();
    var rePassword = $("#rePassword").val();
    var code = $("#code").val();

    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(17[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
    if (phone == '') {
        alert("请输入手机号");
        btn.removeClass('disabled');
        return;
    }
    if(!myreg.test(phone)){
        alert("请输入正确的手机号");
        btn.removeClass('disabled');
        return;
    }
    if (code == '') {
        alert("请输入验证码");
        btn.removeClass('disabled');
        return;
    }
    if (password == '') {
        alert("请输入登录密码");
        btn.removeClass('disabled');
        return;
    }
    if (rePassword == '') {
        alert("请输入确认密码");
        btn.removeClass('disabled');
        return;
    }

    if (rePassword != password) {
        alert("两次密码输入不一致");
        btn.removeClass('disabled');
        return;
    }

    $.ajax({
        type : "POST",
        url : "/resetPwd",
        contentType : "application/x-www-form-urlencoded; charset=utf-8",
        dataType : "json",
        data : {
            phone : phone,
            code : code,
            password : password
        },
        success : function(ret) {
            if (ret.isOk) {
                alert("修改成功");
                location.href = "/";
            }if (ret.isFail) {
                alert(ret.msg);
                btn.removeClass('disabled');
            }
        }
    })
}